package com.leo.ktvplayer.ui.setting;

import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.leo.ktvplayer.R;
import com.leo.ktvplayer.base.App;
import com.leo.ktvplayer.bluetooth.LeProxy;
import com.leo.ktvplayer.domain.bean.Setting;
import com.leo.ktvplayer.localcookie.LocalUtils;

import java.util.ArrayList;

/**
 * Created by LeoLu on 2017/5/3.
 */

public class BleScanResultDialog extends AlertDialog implements View.OnClickListener ,AdapterView.OnItemClickListener {

    private ArrayList<BluetoothDevice> devices = new ArrayList<>();
    private BluetoothResultAdapter resultAdapter;
    private BluetoothManager manager;
    private BluetoothAdapter bluetoothAdapter;
    private ListView lv_ScanResultList;


    public BleScanResultDialog(Context context) {
        super(context);
        init();
    }

    public BleScanResultDialog(Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    protected BleScanResultDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    private void init(){
        View view = getLayoutInflater().inflate(R.layout.dialog_bluetooth_scan_result,null);
        lv_ScanResultList = (ListView)view.findViewById(R.id.lv_dialog_bluetooth_scan_result);
        lv_ScanResultList.setOnItemClickListener(this);
        Button btn_scan = (Button)view.findViewById(R.id.btn_dialog_bluetooth_scan_result_scanbutton);
        btn_scan.setOnClickListener(this);
        resultAdapter = new BluetoothResultAdapter(devices);
        lv_ScanResultList.setAdapter(resultAdapter);
        manager = (BluetoothManager) App.getInstance().getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = manager.getAdapter();
        setCancelable(false);
        setView(view);
    }


    @Override
    public void onClick(View v) {
        bluetoothAdapter.startLeScan(scanCallback);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        bluetoothAdapter.stopLeScan(scanCallback);//停止搜索
        String macress = devices.get(position).getAddress();//选中蓝牙连接
        LeProxy.getInstance().connect(macress, false);
        dismiss();

        Setting setting = LocalUtils.getSetting();
        setting.setBleMacAddress(macress);

        LocalUtils.saveSetting(setting);



    }

    private BluetoothAdapter.LeScanCallback scanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            //TODO scan something... you should add device object to your arraylist<BluetoothDevice>
            if (!devices.contains(device))
                devices.add(device);
            resultAdapter.notifyDataSetChanged();
        }
    };


}
